package tank02;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;

import static tank02.MyGame.*;
import static tank02.Tank.*;

/**
 * 游戏面板
 */
public class MyPanel extends JPanel implements KeyListener,Runnable {
   //英雄对象
    private Hero hero = null;
   //敌人集合
    private ArrayList<Enemy> enemies = new ArrayList<>();

    public MyPanel() {
        //创建英雄坦克对象
    hero    =new Hero(10, 10, Color.CYAN);
        //创建5个敌人
        Color[] colors = {Color.GREEN,Color.MAGENTA,Color.BLUE,Color.ORANGE,Color.RED};
        for(int i = 0;i < 5;i++){
            //创建敌人对象
            Enemy enemy = new Enemy(100 * i,100,colors[i]);
            //添加敌人集合中
            enemies.add(enemy);
            //随机移动敌人
            enemy.randomMove();
            //随机发射子弹
            enemy.randomFire();
            //使用线程池启动敌人
            Tank.threadPool.execute(enemy);
        }
        //线程不断重绘界面
        new Thread(()->{
            while (true){
                this.repaint();
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
    //重写绘制方法
    @Override
    public void paint(Graphics g ) {
        super.paint(g);
        //填充游戏背景界面
        g.setColor(Color.BLACK);
        g.fillRect(0,0,600,800);
        //绘制坦克
        hero.draw(g);
        //绘制英雄
        g.setColor(Color.BLACK);
        g.fillRect(0,0,PANEL_WIDTH,PANEL_HEIGHT);
        hero.draw(g);
        //绘制敌人所有人
        for (int i = 0;i < enemies.size();i++){
            Enemy enemy = enemies.get(i);
            enemy.draw(g);
        }
    }

    //实现按键事件
    @Override
    public void keyPressed(KeyEvent e) {
        //判断玩家的按键
        switch (e.getKeyCode()) {
            case KeyEvent.VK_UP:
                //修改坦克方向为上
                hero.setDirection(DIRECTION_UP);
                break;
            case KeyEvent.VK_DOWN:
                hero.setDirection(DIRECTION_DOWN);
                break;
            case KeyEvent.VK_LEFT:
                hero.setDirection(DIRECTION_LEFT);
                break;
            case KeyEvent.VK_RIGHT:
                hero.setDirection(DIRECTION_RIGHT);
                break;
        }

    }
    @Override
            public void run() {
        while (true){
        //玩家坦克移动
        hero.move();
        //重绘面板
        repaint();
        //睡眠100秒
            try {
                Thread.sleep(100);
            }catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }


    @Override
    public void keyTyped(KeyEvent e) {
        System.out.println(e.getKeyChar());
        switch (e.getKeyCode()) {
            case KeyEvent.VK_UP:
                hero.setDirection(DIRECTION_UP);
                hero.move();
                break;
            case KeyEvent.VK_DOWN:
                hero.setDirection(DIRECTION_DOWN);
                hero.move();
                break;
            case KeyEvent.VK_LEFT:
                hero.setDirection(DIRECTION_LEFT);
                hero.move();
                break;
            case KeyEvent.VK_RIGHT:
                hero.setDirection(DIRECTION_RIGHT);
                hero.move();
                break;
        }

    }

    @Override
    public void keyReleased(KeyEvent e) {

    }
}